<template>
  <div class="login-container">
    <div class="login-form">
      <h1>小兔子</h1>
      <form @submit.prevent="handleSubmit">
        <div class="form-group">
          <label for="username">用户名</label>
          <input v-model="form.username" type="text" id="username" placeholder="请输入用户名" required />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input v-model="form.password" type="password" id="password" placeholder="请输入密码" required />
        </div>
        <!-- <div class="form-group">
          <label for="captcha">验证码</label>
          <input
            v-model="userInput"
            type="text"
            id="captcha"
            placeholder="请输入验证码"
            required
          />
          <img :src="captchaImage" alt="Captcha" @click="refreshCaptcha" />
        </div> -->
        <button type="submit" @click="handleSubmit()">登录</button>
      </form>
    </div>
  </div>
</template>

<script lang="ts" setup>
// 引入组件
import { ref, reactive, inject } from 'vue';
// 数据访问
import { type AxiosStatic } from 'axios';
import { ElMessage, type ComponentSize, type FormInstance } from 'element-plus';
// 引入pinia
import { useRouter } from 'vue-router';

const router = useRouter();

const formSize = ref<ComponentSize>('default');
const ruleFormRef = ref<FormInstance>();
const axios = inject('axios') as AxiosStatic;

const form = reactive({
  username: '',
  password: '',
});

const handleSubmit = () => {
  axios.post('User/login', form)
    .then(res => {
      if (res.data.data != null) {
        ElMessage.success(res.data.data.userName + ' 登录成功',)
        router.push('/RabbitView');
      } else {
        ElMessage({
          message: res.data.msg,
          type: 'error',
        });
      }
    })
    .catch(error => {
      // 处理错误
    });
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('../assets/R-C.jpg');
  /* 背景图路径 */
  background-size: cover;
  /* 背景图覆盖整个容器 */
  background-position: center;
  /* 背景图居中 */
  background-repeat: no-repeat;
  /* 背景图不重复 */
}

.login-form {
  background: rgba(255, 255, 255, 0.9);
  /* 设置透明度以便更好地显示背景图 */
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h1 {
  margin-bottom: 20px;
  font-size: 24px;
  text-align: center;
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
}

.form-group input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

button {
  width: 100%;
  padding: 10px;
  background: #007bff;
  border: none;
  border-radius: 4px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}

button:hover {
  background: #0056b3;
}

.error-message {
  color: #e74c3c;
  text-align: center;
}
</style>